﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Metro.Admin.Views;
using Microsoft.Practices.ObjectBuilder;
using Metro.Admin.BusinessEntities;

public partial class Admin_Default : Microsoft.Practices.CompositeWeb.Web.UI.Page, IDefaultView
{
    private DefaultViewPresenter _presenter;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            this._presenter.OnViewInitialized();
        }
        this._presenter.OnViewLoaded();
    }

    [CreateNew]
    public DefaultViewPresenter Presenter
    {
        set
        {
            this._presenter = value;
            this._presenter.View = this;
        }
    }

    #region IDefaultView Members

    public void UpdateCustomer(Metro.Admin.BusinessEntities.Customer customer)
    {
        this._presenter.UpdateCustomer(customer); 
    }

    public void CreateCustomer(Metro.Admin.BusinessEntities.Customer customer)
    {
        this._presenter.CreateCustomer(customer); 
    }

    public void DeleteCustomer(Metro.Admin.BusinessEntities.Customer customer)
    {
        this._presenter.DeleteCustomer(customer);
    }

    public void ShowCustomers(System.Collections.Generic.IList<Metro.Admin.BusinessEntities.Customer> customers)
    {
        CustomerContainerDataSource.DataSource = customers;
        customerGrid.Visible = true;

        CustomersContainerDataSource.DataSource = customers;  
    }

    public System.Collections.Generic.IList<Customer> ShowCustomers()
    {
        return this._presenter.ShowCustomers();
    }

    public void ShowCities(System.Collections.Generic.ICollection<Metro.Admin.BusinessEntities.City> cities)
    {
        CityContainerDataSource.DataSource = cities;

        ddlCity.Items.Clear();   
        ListItem li = null;
        foreach (City city in cities)
        {
            li = new ListItem(city.Name + " (" + city.Country.Name + ")", city.Id);
            ddlCity.Items.Add(li);  
        }
    }
    
    public void UpdateUser(Metro.Admin.BusinessEntities.User user)
    {
        this._presenter.UpdateUser(user);
        this._presenter.OnViewLoaded();
    }

    public void CreateUser(Metro.Admin.BusinessEntities.User user)
    {
        this._presenter.CreateUser(user);
    }

    public void DeleteUser(Metro.Admin.BusinessEntities.User user)
    {
        this._presenter.DeleteUser(user);
    }

    public void ShowUsers(System.Collections.Generic.IList<Metro.Admin.BusinessEntities.User> users)
    {
        UserContainerDataSource.DataSource = users;
        userGrid.Visible = true;
    }

    #endregion

    protected void CustomerContainerDataSource_Deleted(object sender, Microsoft.Practices.Web.UI.WebControls.ObjectContainerDataSourceStatusEventArgs e)
    {
        this.DeleteCustomer((Metro.Admin.BusinessEntities.Customer)e.Instance);
        this._presenter.OnViewLoaded();
    }

    protected void CustomerContainerDataSource_Updated(object sender, Microsoft.Practices.Web.UI.WebControls.ObjectContainerDataSourceStatusEventArgs e)
    {
        this.UpdateCustomer((Metro.Admin.BusinessEntities.Customer)e.Instance);
        this._presenter.OnViewLoaded();
    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        try 
        {
            this.CreateCustomer();
            this._presenter.OnViewLoaded();
        }
        catch (Exception ex)
        {
            Trace.Warn("Default.aspx - btnAdd_Click", ex.Message);   
        }
    }

    private void CreateCustomer()
    {
        Guid id = Guid.NewGuid();
        string idCity = string.Empty;
        string cityName=string.Empty;
 
        try
        {
            idCity =  ddlCity.SelectedItem.Value ;
            cityName = ddlCity.SelectedItem.Text;
   
            Metro.Admin.BusinessEntities.Customer customer = new Metro.Admin.BusinessEntities.Customer();

            customer.Id = id.ToString();
            customer.Name = txtName.Text;
            customer.SureName = txtLastName.Text;
            customer.State = CustomerServices.GetState(ddlState.SelectedValue);
            customer.DocumentNumber = txtDocument.Text;
            customer.DocumentType = ddlDocType.SelectedItem.Text;
            customer.CustomerType = CustomerServices.GetCustomerType(ddlCustomerType.SelectedValue);
            customer.CityId = idCity;
            customer.CityName = cityName;
            customer.TechnicalName = CustomerServices.GetTechnicalName();
            customer.Commission = decimal.Parse(txtCommission.Text);
   
            this.CreateCustomer(customer);

            txtName.Text = string.Empty;
            txtLastName.Text = string.Empty;
            txtDocument.Text = string.Empty;
             
        }
        catch (Exception ex)
        {
            throw ex; 
        }
    }
    
    protected void UserContainerDataSource_Deleted(object sender, Microsoft.Practices.Web.UI.WebControls.ObjectContainerDataSourceStatusEventArgs e)
    {
        this.DeleteUser((Metro.Admin.BusinessEntities.User)e.Instance);
    }
    
    protected void UserContainerDataSource_Updated(object sender, Microsoft.Practices.Web.UI.WebControls.ObjectContainerDataSourceStatusEventArgs e)
    {
        this.UpdateUser((Metro.Admin.BusinessEntities.User)e.Instance);
    }

    protected void btnAddUser_Click(object sender, EventArgs e)
    {
        try
        {
            this.CreateUser();
            this._presenter.OnViewLoaded();
        }
        catch (Exception ex)
        {
            
            throw ex;
        }
    }

    private void CreateUser()
    {
        try
        {
            Metro.Admin.BusinessEntities.User user = new Metro.Admin.BusinessEntities.User();
            user.Name = txtUserName.Text;
            user.LastName = txtUserLastName.Text;
            user.Login = txtLogin.Text;
            user.Password = txtPassword.Text;
            user.CustomerId = ddlCustomer.SelectedItem.Value;
            user.UserTypeString = ddlUserType.SelectedItem.Value;
            user.Email = txtEmail.Text;
            user.PasswordQuestion = txtPasswordQuestion.Text;
            user.PasswordAnswer = txtPasswordAnswer.Text;   

            this.CreateUser(user);

            txtUserName.Text = string.Empty; 
            txtUserLastName.Text = string.Empty; 
            txtLogin.Text=string.Empty; 
            txtPassword.Text=string.Empty; 
            txtRepeatPassword.Text = string.Empty;
            txtEmail.Text = string.Empty;
            txtPasswordQuestion.Text = string.Empty;
            txtPasswordAnswer.Text = string.Empty;   
        }
        catch (Exception ex)
        {
            throw ex;
        }
        
    
    }
}

